// app/model/brand_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const Brand = app.model.define('brand', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: STRING(100),
      allowNull: false,
      unique: true,
      comment: '品牌名称'
    },
    description: {
      type: STRING(500),
      comment: '品牌描述'
    },
    status: {
      type: STRING(20),
      defaultValue: 'active',
      comment: '品牌状态: active-启用, inactive-禁用'
    },
    createdAt: {
      type: DATE,
      field: 'created_at'
    },
    updatedAt: {
      type: DATE,
      field: 'updated_at'
    }
  }, {
    tableName: 'brands',
    timestamps: true,
    underscored: true // 使用下划线命名
  });

  // 定义与产品的关联关系
  Brand.associate = function() {
    // 确保关联的模型存在
    if (app.model.Product) {
      Brand.hasMany(app.model.ProductModel, {
        foreignKey: 'brand_id',
        as: 'products'
      });
    }
  };

  return Brand;
};